﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace 护士工作站
{
    public partial class 病人信息 : Form
    {
        public 病人信息()
        {
            InitializeComponent();
        }





        private void button_tc_Click(object sender, EventArgs e)
        {
            this.Hide();
            菜单页面 f = new 菜单页面();
            f.Show();
        }

        private void button_bc_Click(object sender, EventArgs e)
        {
            DateTime dt = System.DateTime.Now;
            int year = dt.Year;
            string hosNo = tb_ZYNo.Text.Trim();
            string patName = tb_xm.Text.Trim();
            string hosBalance = tb_ye.Text.Trim();
            string patSex = tb_xb.Text.Trim();
            string patAge = tb_nl.Text.Trim();

            int Age = Convert.ToInt32(patAge);
            int ds = year - Age;
            string bronYear = ds.ToString();

            string hosTotal = tb_fyhj.Text.Trim();
            string bedNo = tb_ch.Text.Trim();
            DateTime hosIndate = dtp_ryrq.Value;
            
            string hosPrepay = tb_yjje.Text.Trim();
            string patType = tb_fb.Text.Trim();
            DateTime hosSurgerydate = dtp_ssrq.Value;
            string hosRank = tb_hldj.Text.Trim();
            string hosCondition = tb_bqzt.Text.Trim();
            DateTime hosOfficeDate = dtp_rkrq.Value;
            string hosIllness = tb_zyzd.Text.Trim();
            string dname = tb_jzys.Text.Trim();
            string nname = tb_zrhs.Text.Trim();
            string a = tb_ch.Text.Trim();
            int b = a.Length;
            string c = a.Substring(0, (b - 1));
            string bedRoom = c;
            string bedSubject = tb_rzks.Text.Trim();
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string DNo = "select DocNo from Doctor where DocName ='" + dname + "' ";
                string NNo = "select NurNo from Nurse where NurName ='" + nname + "' ";
                string BNo = "select BedNo from Bed where BedNo ='" + bedNo + "' ";
                SqlCommand D = new SqlCommand(DNo, conn);
                SqlCommand N = new SqlCommand(NNo, conn);
                SqlCommand B = new SqlCommand(BNo, conn);
                conn.Open();
                string DD = Convert.ToString(D.ExecuteScalar());
                string NN = Convert.ToString(N.ExecuteScalar());
                string BB = Convert.ToString(B.ExecuteScalar());
                string sql = string.Format("update Patient set PatName='" + patName + "',PatSex='" + patSex + "',BronYear='" + bronYear + "'where Patient.HosNo='" + hosNo + "'");
                string str = string.Format("update InHospital set HosIndate='" + hosIndate + "',HosPrepay='" + hosPrepay + "',PatType='" + patType + "',HosSurgerydate='" + hosSurgerydate + "',HosRank='" + hosRank + "',HosCondition='" + hosCondition + "',HosIllness='" + hosIllness + "',HosTotal='" + hosTotal + "',HosBalance='" + hosBalance + "',DocNo='" + DD + "',NurNo='" + NN + "',HosOfficeDate='" + hosOfficeDate + "'where InHospital.HosNo='" + hosNo + "'");
                string abc = string.Format("update Bed set BedCondition='已用'where Bed.BedNo='" + BB + "'");
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlCommand com = new SqlCommand(str, conn);
                SqlCommand bed = new SqlCommand(abc, conn);

                int n = Convert.ToInt32(cmd.ExecuteNonQuery());
                int m = Convert.ToInt32(com.ExecuteNonQuery());
                int z = Convert.ToInt32(bed.ExecuteNonQuery());
                if (n == 1 && m == 1 && z == 1)
                {
                    MessageBox.Show("更新成功！");
                }
                else
                {
                    MessageBox.Show("更新失败！");
                }

            }
        }

        private void button_cx_Click_1(object sender, EventArgs e)
        {
            string ZYNo = tb_ZYNo.Text.Trim();
            string ch = tb_ch.Text.Trim();

            if (ZYNo == "" && ch == "")
            {
                MessageBox.Show("请输入要查询的内容！");
                tb_ZYNo.Focus();
                tb_ch.Focus();

                return;
            }


            string Hos = "select * from InHospital,Patient,Bed where InHospital.HosNo='" + ZYNo + "'and InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";
            string Bed = "select * from Bed ,InHospital,Patient where Bed .BedNo='" + ch + "'and InHospital.HosNo=Patient.HosNo and InHospital.BedNo=Bed.BedNo";

            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {

                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                SqlCommand cmd = new SqlCommand(Hos, conn);
                SqlCommand str = new SqlCommand(Bed, conn);

                if (ZYNo != "")
                {
                    SqlDataReader dr = cmd.ExecuteReader();

                    if (dr.HasRows)
                    {
                        dr.Read();
                        tb_bqzt.Text = dr["HosCondition"].ToString();
                        tb_fyhj.Text = dr["HosTotal"].ToString();

                        int nf = Convert.ToInt32(dr["BronYear"].ToString());
                        DateTime time = System.DateTime.Now;
                        int year = time.Year;
                        int nl = year - nf;
                        tb_nl.Text = nl.ToString();
                        dtp_ryrq.Value = Convert.ToDateTime(dr["HosIndate"]);
                        tb_fb.Text = dr["PatType"].ToString();

                        tb_xb.Text = dr["PatSex"].ToString();
                        tb_xm.Text = dr["PatName"].ToString();
                        tb_ch.Text = dr["BedNo"].ToString();
                        tb_rzks.Text = dr["HosDepartment"].ToString();
                        tb_ye.Text = dr["HosBalance"].ToString();
                        dtp_ssrq.Text = dr["HosSurgerydate"].ToString();
                       
                        tb_yjje.Text = dr["HosPrepay"].ToString();

                        tb_zyzd.Text = dr["HosIllness"].ToString();
                        tb_ZYNo.Text = dr["HosNo"].ToString();
                        tb_hldj.Text = dr["HosRank"].ToString();
                        dtp_rkrq.Text = dr["HosOfficeDate"].ToString();
                        string dno = dr["DocNo"].ToString();
                        string nno = dr["NurNo"].ToString();
                        dr.Close();
                        string NNo = "select NurName from Nurse where NurNo ='" + nno + "' ";
                        string DNo = "select DocName from Doctor where  DocNo='" + dno + "' ";
                        string DGr = "select DocGroup from Doctor where  DocNo='" + dno + "' ";
                        SqlCommand D = new SqlCommand(DNo, conn);
                        SqlCommand N = new SqlCommand(NNo, conn);
                        SqlCommand G = new SqlCommand(DGr, conn);
                        string DD = Convert.ToString(D.ExecuteScalar());
                        string NN = Convert.ToString(N.ExecuteScalar());
                        string GG = Convert.ToString(G.ExecuteScalar());
                        tb_jzys.Text = DD;
                        tb_zrhs.Text = NN; 
                        tb_szzb.Text = GG ;
                    }
                    dr.Close();
                }

                if (ch != "")
                {
                    SqlDataReader ds = str.ExecuteReader();
                    if (ds.HasRows)
                    {
                        ds.Read();
                        tb_bqzt.Text = ds["HosCondition"].ToString();
                        tb_fyhj.Text = ds["HosTotal"].ToString();

                        int nf = Convert.ToInt32(ds["BronYear"].ToString());
                        DateTime time = System.DateTime.Now;
                        int year = time.Year;
                        int nl = year - nf;
                        tb_nl.Text = nf.ToString();
                        dtp_ryrq.Value = Convert.ToDateTime(ds["HosIndate"]);
                        tb_fb.Text = ds["PatType"].ToString();

                        tb_xb.Text = ds["PatSex"].ToString();
                        tb_xm.Text = ds["PatName"].ToString();
                        tb_ch.Text = ds["BedNo"].ToString();
                        tb_rzks.Text = ds["HosDepartment"].ToString();
                        tb_ye.Text = ds["HosBalance"].ToString();
                        dtp_ssrq.Text = ds["HosSurgerydate"].ToString();
                        dtp_ssrq.Value = Convert.ToDateTime(ds["HosSurgerydate"]);
                        tb_szzb.Text = ds["HosArea"].ToString();
                        tb_yjje.Text = ds["HosPrepay"].ToString();
                        tb_ye.Text = ds["HosBalance"].ToString();
                        tb_zyzd.Text = ds["HosIllness"].ToString();
                        tb_ZYNo.Text = ds["HosNo"].ToString();
                        tb_hldj.Text = ds["HosRank"].ToString();
                        dtp_rkrq.Value = Convert.ToDateTime(ds["HosOfficeDate"]);
                        string dno = ds["DocNo"].ToString();
                        string nno = ds["NurNo"].ToString();
                        ds.Close();
                        string NNo = "select NurName from Nurse where NurNo ='" + nno + "' ";
                        string DNo = "select DocName from Doctor where  DocNo='" + dno + "' ";
                        SqlCommand D = new SqlCommand(DNo, conn);
                        SqlCommand N = new SqlCommand(NNo, conn);
                        string DD = Convert.ToString(D.ExecuteScalar());
                        string NN = Convert.ToString(N.ExecuteScalar());
                        tb_jzys.Text = DD;
                        tb_zrhs.Text = NN;
                    }
                    ds.Close();
                }
            }

        }

        private void button_qp_Click(object sender, EventArgs e)
        {
            tb_bqzt.Text = "";
            tb_ch.Text = "";
            tb_fb.Text = "";
            tb_rzks.Text = "";
            tb_nl.Text = "";
            dtp_ryrq.Text = "";
            dtp_ssrq.Text = "";
            tb_szzb.Text = "";
            tb_xb.Text = "";
            tb_xm.Text = "";
            tb_yjje.Text = "";
            tb_jzys.Text = "";
            tb_zyzd.Text = "";
            tb_zrhs.Text = "";
            tb_ZYNo.Text = "";
            tb_hldj.Text = "";
            dtp_rkrq.Text = "";
        }
    }
}
